package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.BannerBusiness;
import com.ticket.sass.admin.config.AppConfig;
import com.ticket.sass.admin.dto.input.banner.BannerAddInput;
import com.ticket.sass.admin.dto.input.banner.BannerListInput;
import com.ticket.sass.admin.dto.input.banner.BannerUpdInput;
import com.ticket.sass.admin.dto.output.banner.BannerListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.BannerPositionServiceImpl;
import com.ticket.sass.admin.service.impl.BannerServiceImpl;
import com.ticket.sass.admin.util.*;
import com.ticket.sass.common.entity.tenant.BannerEntity;
import com.ticket.sass.common.entity.tenant.BannerPositionEntity;
import com.ticket.sass.common.entity.tenant.enums.JumpType;
import com.ticket.sass.common.exception.enums.ErrorCode;
import com.ticket.sass.common.exception.exception.BadRequestException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2024-04-30 11:08
 */
@Slf4j
@Service
@AllArgsConstructor
public class BannerBusinessImpl implements BannerBusiness {
    final AppConfig appConfig;
    final ModelUtil modelUtil;
    final FileUtil fileUtil;
    final BannerServiceImpl service;
    final BannerPositionServiceImpl positionService;
    
    @Override
    public PageModel<BannerListOutput> list(BannerListInput input) {
        // 查询影院分页数据
        IPage<BannerEntity> page = service.iPage(input);
        List<BannerEntity> bannerEntities = page.getRecords();
        
        // 构建输出列表
        List<BannerListOutput> outputs = new ArrayList<>();
        for (BannerEntity bannerEntity : bannerEntities) {
            // 创建输出对象
            BannerListOutput output = new BannerListOutput(bannerEntity);
            output.setPosition(positionService.getPositionArrayByBannerId(output.getId()));
            outputs.add(output);
        }
        
        return new PageModel<>(page.getTotal(), outputs);
    }
    
    @Override
    public void add(BannerAddInput input) {
        fileUtil.tempFieldPro(input, "key");
        BannerEntity bannerEntity = modelUtil.mapToMap(input, BannerEntity.class);
        
        bannerEntity.setJumpConfig(GsonUtil.toJsonString(input.getJumpConfig()));
        bannerEntity.setJumpType(JumpType.Companion.valueOf(input.getJumpType()));
        
        service.save(bannerEntity);
        
        List<BannerPositionEntity> collect = input.getPosition().stream().map(position -> new BannerPositionEntity(bannerEntity.getId(), position)).collect(Collectors.toList());
        positionService.saveBatch(collect);

    }
    
    @Override
    public void upd(BannerUpdInput input) {
        validateId(input.getId());
        
        fileUtil.tempFieldPro(input, "key");
        BannerEntity bannerEntity = modelUtil.mapToMap(input, BannerEntity.class);
        bannerEntity.setJumpConfig(GsonUtil.toJsonString(input.getJumpConfig()));
        bannerEntity.setJumpType(JumpType.Companion.valueOf(input.getJumpType()));
        service.updateById(bannerEntity);
        
        List<Short> oldPosition = positionService.getPositionArrayByBannerId(input.getId());
        List<Short> newPosition = input.getPosition();
        
        CompareUtil<Short> compareUtil = new CompareUtil<>(oldPosition, newPosition);
        if (compareUtil.isInsert()){
            List<BannerPositionEntity> collect = compareUtil.getInsert().stream().map(position -> new BannerPositionEntity(bannerEntity.getId(), position)).collect(Collectors.toList());
            positionService.saveBatch(collect);
        }
        
        if (compareUtil.isDel()){
            positionService.remove(new QueryWrapper<BannerPositionEntity>().eq("banner_id", bannerEntity.getId()).in("position", compareUtil.getDel()));
        }
    }
    
    private void validateId(Long id) {
        if (service.isEmpty(id)) {
            throw new BadRequestException(
                ErrorCode.RECORD_NOT_EXIST,
                EscapeUtil.errorMessage("id", ErrorCode.RECORD_NOT_EXIST.getReasonPhrase())
            );
        }
    }
}
